package com.lvandroid.transport.upload;

import android.util.Log;

import com.arialyy.annotations.Upload;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.task.UploadTask;
import com.lvandroid.transport.task.IOnUploadTaskUpdateListener;
import com.lvandroid.transport.task.UpLoadTaskInfo;


/**
 * Https协议上传器
 */
public class HttpsUpLoader implements IUpLoad {

    private static final String TAG = "HttpsUpLoader";

    private IOnUploadTaskUpdateListener mTaskListener;

    @Override
    public long start(String url, String filePath) {
        long uploadTaskId = Aria.upload(this).load(filePath).setUploadUrl(url).create();
        Log.i(TAG, "HttpsUpLoader -> start upload : " + uploadTaskId);
        return uploadTaskId;
    }

    @Override
    public void stop(long taskId) {
        Log.i(TAG, "HttpsUpLoader -> stop upload.");
        Aria.upload(this).load(taskId).stop();
    }

    @Override
    public void resume(long taskId, boolean reStart) {
        Log.i(TAG, "HttpsUpLoader -> resume upload taskid = " + taskId + ", reStart = " + reStart);
        Aria.upload(this).load(taskId).resume(reStart);
    }

    @Override
    public void addUploadListener(IOnUploadTaskUpdateListener l) {
        mTaskListener = l;
    }

    @Upload.onTaskRunning protected void onTaskUpdate(UploadTask task) {
        //任务下载更新
        Log.i(TAG, "onTaskUpdate : " + task);
        if(null != mTaskListener) {
            UpLoadTaskInfo info = new UpLoadTaskInfo(task);
            mTaskListener.onUpdate(info);
        }
    }

    @Upload.onTaskComplete void onTaskComplete(UploadTask task) {
        //在这里处理任务完成的状态
        Log.i(TAG, "onTaskComplete : " + task);
        if(null != mTaskListener) {
            UpLoadTaskInfo info = new UpLoadTaskInfo(task);
            mTaskListener.onComplete(info);
        }
    }

}
